﻿using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Tools.Ribbon;
using System;
using System.IO;

namespace VSTODemo {
    public partial class Ribbon1 {
        
        private void Ribbon1_Load(object sender, RibbonUIEventArgs e) {
        }

        private void btnImportEmployees_Click(object sender, RibbonControlEventArgs e) {
            Worksheet targetSheet = ThisAddIn.ExcelApp.ActiveSheet;

            // copy worksheet from template file
            string baseDir = AppDomain.CurrentDomain.BaseDirectory;
            string templateFile = $@"{baseDir}templates\emp_master_template.xlsx";

            if (File.Exists(templateFile)) {
                ExcelUtils.CopyWorksheetTemplate(templateFile, "emp_master", targetSheet);
                targetSheet = ThisAddIn.ExcelApp.Worksheets[1]; // 拷贝后，新的工作表位于最开始位置
            }
            else { // if template file does not exist, add a new worksheet
                targetSheet = ThisAddIn.ExcelApp.Worksheets.Add();
            }

            // copy data
            System.Data.DataTable values = EmployeeService.ListAll();
            ExcelUtils.CopyFromDataTable(values, targetSheet);
        }
    }
}
